

import com.sb.mapper.SysUserMapper;
import com.sb.pojo.SysUser;
import com.sb.utils.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

public class SysUserMapperTest {
 private Logger logger = Logger.getLogger(SysUserMapperTest.class);

 SqlSession sqlSession = MyBatisUtil.createSqlSession();

 @Test
 /**
  * 测试if标签
  * 查询用户列表(分页查询)
  */
 public void testSelectList() throws Exception {
  List<SysUser> userList1 = null;
  try {
   // 1.读取MyBatis框架核心配置文件
   String resource = "mybatis-config.xml";
   // 获取对mybatis-config.xml的输入流
   InputStream is = Resources.getResourceAsStream(resource);
   // 2.使用SqlSessionFactoryBuilder读取配置文件并构建
   // SqlSessionFactory实例
   SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
   // 3.创建SqlSession实例
   sqlSession = factory.openSession();
   // 4.创建SysUserMapper接口实例,调用其方法执行相关的SQL语句
   // 测试方法:
   // selectList: if标签
   // selectList1: where标签
   String realName = "赵";
   Integer roleId = 2;
   userList1 = sqlSession.getMapper(SysUserMapper.class).selectList1(realName,roleId);
  } finally {
   MyBatisUtil.closeSqlSession(sqlSession);
  }
  if (userList1 != null) {
   System.out.println("查询到用户数量:" + userList1.size());
   for (SysUser user1 : userList1) {
    System.out.println("查询到用户信息:" + user1);
   }
  }
 }

 @Test
 /**
  * 测试choose(when,otherwise)标签
  * 查询用户列表
  */
 public void testGetUserListByChoose() throws Exception {
  List<SysUser> userList2 = new ArrayList<SysUser>();
  try {
   String realName = "";
   Integer roleId = 2;
   String account = "zhong";
   Date createdTime = new SimpleDateFormat("yyyy-MM-dd").parse("2024-11-11");
   userList2 = sqlSession.getMapper(SysUserMapper.class).selectListByChoose(realName,roleId,account,createdTime);
  }catch (Exception e) {
   e.printStackTrace();
  }finally {
   MyBatisUtil.closeSqlSession(sqlSession);
  }
  logger.info("查询到用户数量:" + userList2.size());
  for (SysUser user2 : userList2) {
   logger.info("查询到用户信息:" + user2);
  }
 }

 @Test
 /**
  * 测试foreach标签
  * 数组类型参数
  * 根据角色id数组 查询用户列表信息
  */
 public void testGetUserByRoleIdByArray() throws Exception {
  List<SysUser> userList3 = new ArrayList<SysUser>();
  Integer[] roleIds = {1,2};
  try {
   userList3 = sqlSession.getMapper(SysUserMapper.class).getUserByRoleIdArray(roleIds);
  }catch (Exception e) {
   e.printStackTrace();
  }finally {
   MyBatisUtil.closeSqlSession(sqlSession);
  }
  logger.info("查询到用户数量:" + userList3.size());
  for (SysUser user3 : userList3){
   logger.info("查询到用户信息:" + user3);
  }
 }

 @Test
 /**
  * 测试foreach标签
  * List类型参数
  * 根据角色id数组 查询用户列表信息
  */
 public void testGetUserByRoleIdByList() throws Exception {
  List<SysUser> userList4 = new ArrayList<>();
  List<Integer> roleList = new ArrayList<Integer>();
  roleList.add(1);
  roleList.add(2);
  userList4 = sqlSession.getMapper(SysUserMapper.class).getUserByRoleIdList(roleList);
  try {
   userList4 = sqlSession.getMapper(SysUserMapper.class).getUserByRoleIdList(roleList);
  }catch (Exception e) {
   e.printStackTrace();
  }finally {
   MyBatisUtil.closeSqlSession(sqlSession);
  }
  logger.info("查询到用户数量:" + userList4.size());
  for (SysUser user4 : userList4){
   logger.info("查询到用户信息:" + user4);
  }
 }

 @Test
 /**
  * 测试foreach标签
  * Map类型参数
  * 根据角色ID集合(集合存在Map中) 查询用户列表信息
  */
 public void testGetUserByRoleIdMap() throws Exception {
  List<Integer> roleIdList = new ArrayList<Integer>();
  roleIdList.add(1);
  roleIdList.add(2);
  Map<String, Object> roleMap = new HashMap<String,Object>();
  roleMap.put("roleIdList",roleIdList);
  List<SysUser> userList5 = null;
  try {
   userList5 = sqlSession.getMapper(SysUserMapper.class).getUserByRoleIdMap(roleMap);
  }catch (Exception e) {
   e.printStackTrace();
  }finally {
   MyBatisUtil.closeSqlSession(sqlSession);
  }
  logger.info("查询到用户数量:" + userList5.size());
  for (SysUser user5 : userList5){
   logger.info("查询到用户信息:" + user5);
  }
 }



 @Test
 /**
  * MyBatis框架动态SQL处理更新能力
  * set+if标签
  */
 public void testUpdate() throws Exception {
  Integer userId = 17;
  int count = 0;
  try {
   SysUser user = new SysUser();
   user.setId(userId);
   user.setRealName("测试用户修改");
   user.setUpdatedUserId(1);
   user.setUpdatedTime(new Date());
   count = sqlSession.getMapper(SysUserMapper.class).update(user);
   sqlSession.commit();
  } catch (Exception e) {
   e.printStackTrace();
   sqlSession.rollback();
  } finally {
   MyBatisUtil.closeSqlSession(sqlSession);
  }
  logger.info("修改Id=" +userId + "的用户修改" + (count==0? "失败" : "成功") );
 }



 @Test
 /**
  * MyBatis框架的分页功能
  */
 public void testGetUserPage() {
  List<SysUser> userList6 = new ArrayList<SysUser>();
  try {
   String realName = "";
   Integer roleId = 2;
   Integer pageIndex = 2;
   Integer pageSize = 2;
   Integer pageBegin = (pageIndex - 1) * pageSize;   //计算查询起始位置
   userList6 = sqlSession.getMapper(SysUserMapper.class).selectPageList(realName,roleId,pageBegin,pageSize);
  }catch (Exception e) {
   e.printStackTrace();
  }finally {
   MyBatisUtil.closeSqlSession(sqlSession);
  }
  logger.info("查询到用户数量:" + userList6.size());
  for (SysUser user6 : userList6)
   logger.info("查询到用户信息:" + user6);
 }
}
